package com.kgc.server.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Random;
@Controller
public class EmailController {

    @Autowired
    JavaMailSenderImpl mailSender;

    @RequestMapping("postEmail")
    @ResponseBody
    @CrossOrigin
    public String postEmail(String email) throws MessagingException {
            String qqEmail = email;
            String codeNum = "";
            int count=1;//默认发送一次
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
            while(count--!=0) {
                int[] code = new int[3];
                Random random = new Random();
                //自动生成验证码
                for (int i = 0; i < 6; i++) {
                    int num = random.nextInt(10) + 48;
                    int uppercase = random.nextInt(26) + 65;
                    int lowercase = random.nextInt(26) + 97;
                    code[0] = num;
                    code[1] = uppercase;
                    code[2] = lowercase;
                    codeNum += (char) code[random.nextInt(3)];
                }
                System.out.println(codeNum);
                //标题
                helper.setSubject("您的验证码为：" + codeNum);
                //内容
                helper.setText("您好!，感谢使用易买网。您的验证码为：" + "<h2>" + codeNum + "</h2>" + "千万不能告诉别人哦！", true);
                //邮件接收者
                helper.setTo(qqEmail);
                //邮件发送者，必须和配置文件里的一样，不然授权码匹配不上
                helper.setFrom("1049769534@qq.com");
                mailSender.send(mimeMessage);
            }
        return codeNum;
    }
}
